package sdfi.lee.Listener;

import javax.jms.Destination;
import javax.jms.JMSException;
import javax.jms.Session;
import javax.servlet.http.HttpSession;
import javax.servlet.http.HttpSessionEvent;
import javax.servlet.http.HttpSessionListener;

import org.apache.activemq.ActiveMQConnection;
import org.apache.activemq.command.ActiveMQDestination;

import sdfi.lee.JMS.ConnectionFactory;

/**
 * Application Lifecycle Listener implementation class DeleteQueue
 *
 */
public class DeleteQueue implements HttpSessionListener {

    /**
     * Default constructor. 
     */
    public DeleteQueue() {
        // TODO Auto-generated constructor stub
    }

	/**
     * @see HttpSessionListener#sessionCreated(HttpSessionEvent)
     */
    public void sessionCreated(HttpSessionEvent arg0) {
        // TODO Auto-generated method stub
    }

	/**
     * @see HttpSessionListener#sessionDestroyed(HttpSessionEvent)
     */
    public void sessionDestroyed(HttpSessionEvent event) {
        // TODO Auto-generated method stub
    	
    	HttpSession session=event.getSession();
    	Object obj=session.getAttribute("anqueue");
    	if(obj!=null)
    	{
    		 ActiveMQConnection con = (ActiveMQConnection) ConnectionFactory.getCon();
    		Destination queue=null;
			try {
				queue = con.createSession(false, Session.AUTO_ACKNOWLEDGE).createQueue(obj.toString());
				try {
					con.start();
				} catch (JMSException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
				try {
					con.destroyDestination((ActiveMQDestination) queue);
					
				} catch (JMSException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			
			} catch (JMSException e1) {
				// TODO Auto-generated catch block
				e1.printStackTrace();
			}
			finally
			{
				try {
					con.stop();
				} catch (JMSException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			}
    		
    	}
    }
	
}
